<template>
  <el-card class="box-card">
    <div style="line-height: 0px">
      <el-form :inline="true" class="demo-form-inline" size="small">
        <el-form-item>
          <el-input
            v-model="formInline.roleName"
            clearable
            placeholder="角色姓名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryRoleList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogFormVisible = true"
            >新增</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            type="danger"
            @click="deleteList()"
            :disabled="multipleSelection.length === 0"
            >删除</el-button
          >
        </el-form-item>

        <!-- dialog窗口(新增) -->
        <el-dialog title="新建" :visible.sync="dialogFormVisible" width="40%">
          <el-form ref="addForm" :model="addForm" :rules="rules">
            <el-form-item label="角色名称" prop="roleName" label-width="110px">
              <el-input
                class="form-input"
                v-model="addForm.roleName"
                style="width: 350px"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark" label-width="110px">
              <el-input
                class="form-input"
                v-model="addForm.remark"
                style="width: 350px"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button
              type="primary"
              @click="submitForm(addForm)"
              :loading="addLoading"
              >确 定</el-button
            >
          </div>
        </el-dialog>

        <!-- dialog窗口(编辑) -->
        <el-dialog
          title="编辑"
          :visible.sync="dialogFormVisibleEdit"
          width="40%"
        >
          <el-form ref="rowData" :model="rowData" :rules="rulesEidt">
            <el-form-item label="角色名称" prop="roleName" label-width="110px">
              <el-input
                class="form-input"
                v-model="rowData.roleName"
                style="width: 350px"
              ></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark" label-width="110px">
              <el-input
                class="form-input"
                v-model="rowData.remark"
                style="width: 350px"
                type="textarea"
                :autosize="{ minRows: 2, maxRows: 4 }"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
            <el-button
              type="primary"
              :loading="updateLoading"
              @click="submitUpdate()"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </el-form>
      <el-table
        class="form-table"
        stripe
        :data="roleList"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        :cell-style="{'text-align':'center'}"
        :header-cell-style="{'text-align':'center'}"
      >
        <el-table-column class="table-selection" type="selection" width="55">
        </el-table-column>
        <el-table-column prop="roleId" label="id" width="50"> </el-table-column>
        <el-table-column prop="roleName" label="名称" width="150">
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="240">
        </el-table-column>
        <el-table-column prop="createUserId" label="创建用户" width="180">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="200">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.pageIndex"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="this.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
        >
        </el-pagination>
      </div>
    </div>
  </el-card>
</template>

<script>
export default {
  name: "SysRole",
  data() {
    var validateCheckNameExist = (rule, value, callback) => {
      this.$http
        .get("/sys/sysRole/checkNameExist?roleName=" + value)
        .then((res) => {
          if (res.data.data > 0) {
            callback(new Error("角色名称已存在"));
          } else {
            callback();
          }
        });
    };
    var validateCheckNameExistEidt = (rule, value, callback) => {
      if (value == this.rowData.proRoleName) callback();
      this.$http
        .get("/sys/sysRole/checkNameExist?roleName=" + value)
        .then((res) => {
          if (res.data.data > 0) {
            callback(new Error("用户名称已存在"));
          } else {
            callback();
          }
        });
    };
    return {
      rowData: {
        roleId: "",
        proRoleName: "",
        roleName: "",
        remark: "",
      },
      multipleSelection: [],
      addForm: {
        roleName: "",
        remark: "",
      },
      rulesEidt: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { validator: validateCheckNameExistEidt, trigger: "blur" },
        ],
        remark: [
          { required: false, message: "请输入描述信息", trigger: "blur" },
        ],
      },
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { validator: validateCheckNameExist, trigger: "blur" },
        ],
        remark: [
          { required: false, message: "请输入描述信息", trigger: "blur" },
        ],
      },
      formInline: {
        roleName: "",
      },
      roleList: [],
      pageIndex: 1,
      pageSize: 10,
      total: 0,
      dataListLoading: false,
      dialogFormVisible: false,
      dialogFormVisibleEdit: false,
      addLoading: false,
      updateLoading: false,
    };
  },
  methods: {
    deleteList() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .post("/sys/sysRole/deleteList", this.multipleSelection)
            .then((res) => {
              if (res.data.data > 0) {
                this.$message({
                  type: "success",
                  message: "删除成功!共删除" + res.data.data + "条记录",
                });
                this.queryRoleList();
              } else {
                this.$message({
                  type: "warning",
                  message: "系统繁忙!",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      val.forEach((element) => {
        this.multipleSelection.push(element.roleId);
      });
    },
    handleEdit(index, row) {
      this.rowData.proRoleName = row.roleName;
      this.rowData.roleName = row.roleName;
      this.rowData.roleId = row.roleId;
      this.rowData.remark = row.remark;
      this.dialogFormVisibleEdit = true;
      this.$nextTick(()=>{
        this.$refs['rowData'].clearValidate() // 只清除清除验证
      })
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .get("/sys/sysRole/deleteOne?roleId=" + row.roleId)
            .then((res) => {
              if (res.data.data) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.queryRoleList();
              } else {
                this.$message({
                  type: "warning",
                  message: "系统繁忙!",
                });
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitForm(addForm) {
      this.$refs["addForm"].validate((valid) => {
        if (valid) {
          this.addLoading = true;
          this.$http.post("/sys/sysRole/save", addForm).then((res) => {
            if (res.data.data) {
              this.$message({
                message: "添加角色成功",
                type: "success",
              });
              this.dialogFormVisible = false;
              this.addLoading = false;
              this.addForm = {};
              this.queryRoleList();
            } else {
              this.$message.error({
                message: "系统繁忙",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    submitUpdate() {
      this.$refs["rowData"].validate((valid) => {
        if (valid) {
          this.updateLoading = true;
          this.$http
            .post("/sys/sysRole/updateOne", this.rowData)
            .then((res) => {
              if (res.data.data) {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.updateLoading = false;
                this.dialogFormVisibleEdit = false;
                this.queryRoleList();
              } else {
                this.$message.error({
                  message: "系统繁忙",
                });
              }
              
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.queryRoleList();
    },
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.queryRoleList();
    },
    queryRoleList() {
      if (this.dataListLoading) return;
      this.dataListLoading = true;
      const params = {
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          roleName: this.formInline.roleName,
        },
      };
      this.$http.get("/sys/sysRole/list", params).then((res) => {
        this.roleList = res.data.data.list;
        this.total = res.data.data.totalCount;
        this.dataListLoading = false;
      });
    },
  },
  mounted() {
    this.queryRoleList();
  },
};
</script>

<style lang="less" scoped>
.form-input {
  margin-left: -100px;
}
/deep/ .el-form-item__error {
  margin-left: -100px;
}
/deep/ .cell {
  padding-left: 10px;
}
</style>